package com.girant.logger.controller.view;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.girant.logger.entity.dto.BusinessLogDTO;
import com.girant.logger.entity.dto.BusinessLogExportDTO;
import com.girant.logger.entity.vo.form.LogFormExportVO;
import com.girant.logger.entity.vo.form.LogPageFormVO;
import com.girant.logger.entity.vo.show.LogExportVO;
import com.girant.logger.entity.vo.show.LogShowVO;
import com.girant.model.utils.Result;
import com.girant.mybatis.entity.BasePage;
import com.girant.logger.service.LogInfoService;
import com.girant.logger.service.LogExportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.girant.excel.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

@Tag(name = "操作日志接口")
@RestController
@RequestMapping("/view/v1/log/info")
public class LogController {
    private static final Logger log = LoggerFactory.getLogger(LogController.class);
    @Autowired
    private LogInfoService logInfoService;
    // 关键：注入LogExportService实例（非静态调用）
    @Autowired
    private LogExportService logExportService;

//    @Value("${xxx.xxx}")
//    private Integer xx;



    // 普通日志分页查询
    @Operation(summary = "操作日志分页列表", description = "根据传入的查询条件（如时间范围、服务名称、错误级别等），分页返回日常日志列表")
    @PostMapping("/loggedPage")
    //@CheckPermission("sys:log:query:info")
    public Result<BasePage<LogShowVO>> searchLogs(@Validated LogPageFormVO request) {
        BusinessLogDTO requestDTO = BeanUtil.copyProperties(request, BusinessLogDTO.class);
            BasePage<LogShowVO> pageData = logInfoService.queryLogPage(requestDTO);
            return Result.success(pageData);
    }



    //日志导出
    @SneakyThrows
    @Operation(summary = "操作日志列表excel导出", description = "根据传入的查询条件（如时间范围、服务名称、标题等），导出Excel")
    @PostMapping("/loggedExport")
    @Validated
    //@CheckPermission("sys:log:query:info")
    public void businessLogsExport(@Validated LogFormExportVO request, HttpServletResponse response) {
        // 1. 转化成DTO，查询导出数据
        BusinessLogExportDTO requestDTO = BeanUtil.copyProperties(request, BusinessLogExportDTO.class);

        List<LogExportVO> excelResource = logExportService.exportLog(requestDTO);

            // 2. 生成符合业务的文件名（修正文件名）
            String fileName = generateDocExportFileName("日常日志导出");

            // 3. 输出Excel到响应流（确保工具类正确处理response）
            ExcelUtil.write(LogExportVO.class, response, fileName, excelResource);
    }
    public static String generateDocExportFileName(String docName) {
        return docName + DateUtil.format(new Date(), "yyyy-MM-dd");
    }
}
